package thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Demo31 {
    public static void main(String[] args) {
        //至少生产者一个线程，消费者一个线程

        BlockingDeque queue = new LinkedBlockingDeque();

        Thread producer = new Thread(()->{
            int n = 0;
            while(true){
                try {
                    queue.put(n);
                    System.out.println("生产元素 "+ n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {


                }
            }
        },"producer");

        Thread consumer = new Thread(()->{
            while(true){
                try {
                    Integer n = (Integer) queue.take();
                    System.out.println("消费元素" + n);
                    //Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"consumer");
    }
}
